// pages/discover/discover.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: [],
    songSheet: [],
    hotTags: [],
    hotSheetName: '',
    playList: [],
    count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const type = this._getSystemType()

    const res = await request(`/banner?type=${type}`)
    this.setData({
      banners: res.banners.map((item) => {
        return {
          pic: item.pic,
          titleColor: item.titleColor,
          typeTitle: item.typeTitle,
          id: item.bannerId
        }
      })
    })
  },
  // 获取操作系统
  _getSystemType() {
    let type = 0
    const res = wx.getSystemInfoSync()
    if (res.system.includes('IOS') > -1) {
      type = 3
      if (res.model.includes('iPhone') > -1) {
        type = 2
      }
    } else if (res.system.includes('Android') > -1) {
      type = 1
    }
    return type
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this._getSongSheet()
    this._getHotTags()
    // this._getHotSheet()
  },
  // 获取推荐歌单
  async _getSongSheet() {
    const res = await request(`/personalized?limit=${10}`)
    this.setData({
      songSheet: res.result.map((item) => {
        return {
          id: item.id,
          name: item.name,
          picUrl: item.picUrl,
          playCount: Math.round(item.playCount / 10000)
        }
      }),
    })
  },
  // 获取热门歌单标签
  async _getHotTags() {
    const res = await request('/playlist/highquality/tags')
    this.setData({
      hotTags: res.tags.splice(0, 5).map((item) => item.name)
    })
    this._getHotSheet(this.data.count)
  },
  // 获取歌单对应歌曲
  async _getHotSheet(index) {
    const result = await request(`/top/playlist/highquality`, {
      cat: this.data.hotTags[index],
      limit: 1
    })
    const {
      id,
      name
    } = result.playlists[0]
    this.setData({
      hotSheetName: name
    })
    // 获取歌曲
    const response = await request(`/playlist/track/all?id=${id}&limit=12`)

    const list = response.songs.map((item) => {
      return {
        name: item.name,
        singer: item.ar.map((item) => item.name).join(' / '),
        picUrl: item.al.picUrl,
        desc: item.al.name,
        id: item.id
      }
    })
    const length = list.length / 3
    const playList = []
    for (let i = 0; i < length; i++) {
      playList.push({
        id: i + 1,
        songs: list.splice(0, 3)
      })
    }
    this.setData({
      playList
    })
  },
  // 刷新切换歌单
  refreshPlayList() {
    this.animate('.icon-shuaxin', [{ rotate: 0 }, { rotate: 360 }], 500);

    let count = this.data.count + 1
    if (count >= this.data.hotTags.length) {
      count = 0
    }
    this.setData({
      count
    })
    this._getHotSheet(count)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})